package org.ruoyi.knowledge.util;

import org.ruoyi.knowledge.service.RagflowService;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * RAGFlow维护工具类
 * 用于调试和修复数据不一致问题
 * 
 * @author Lion Li
 * @date 2025-01-27
 */
@Component
public class RagflowMaintenanceUtil {

    private final RagflowService ragflowService;

    public RagflowMaintenanceUtil(RagflowService ragflowService) {
        this.ragflowService = ragflowService;
    }

    /**
     * 手动删除指定名称的数据集
     * 
     * @param datasetName 数据集名称
     * @return 是否成功
     */
    public boolean forceDeleteDataset(String datasetName) {
        try {
            System.out.println("开始强制删除数据集: " + datasetName);
            
            // 1. 获取数据集ID
            String datasetId = ragflowService.getDatasetIdByName(datasetName);
            
            if (datasetId == null || datasetId.isEmpty()) {
                System.out.println("未找到数据集: " + datasetName);
                return false;
            }
            
            System.out.println("找到数据集ID: " + datasetId);
            
            // 2. 删除数据集
            boolean success = ragflowService.deleteDataset(datasetId);
            
            if (success) {
                System.out.println("成功删除数据集: " + datasetName + " (ID: " + datasetId + ")");
            } else {
                System.out.println("删除数据集失败: " + datasetName);
            }
            
            return success;
        } catch (Exception e) {
            System.err.println("强制删除数据集失败: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 列出所有RAGFlow数据集
     * 
     * @return 数据集列表
     */
    public void listAllDatasets() {
        try {
            List<Map<String, Object>> datasets = ragflowService.getAllDatasets();
            
            if (datasets == null || datasets.isEmpty()) {
                System.out.println("RAGFlow中没有任何数据集");
                return;
            }
            
            System.out.println("========== RAGFlow数据集列表 ==========");
            System.out.println("共 " + datasets.size() + " 个数据集");
            System.out.println();
            
            for (Map<String, Object> dataset : datasets) {
                String name = (String) dataset.get("name");
                String id = (String) dataset.get("id");
                System.out.println("数据集名称: " + name);
                System.out.println("数据集ID:   " + id);
                System.out.println("--------------------------------------------");
            }
            
        } catch (Exception e) {
            System.err.println("获取数据集列表失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 删除所有RAGFlow数据集（危险操作）
     * 
     * @return 删除的数据集数量
     */
    public int deleteAllDatasets() {
        try {
            List<Map<String, Object>> datasets = ragflowService.getAllDatasets();
            
            if (datasets == null || datasets.isEmpty()) {
                System.out.println("没有可删除的数据集");
                return 0;
            }
            
            System.out.println("警告：准备删除 " + datasets.size() + " 个数据集");
            
            int deletedCount = 0;
            for (Map<String, Object> dataset : datasets) {
                String name = (String) dataset.get("name");
                String id = (String) dataset.get("id");
                
                System.out.println("正在删除: " + name + " (ID: " + id + ")");
                
                if (ragflowService.deleteDataset(id)) {
                    deletedCount++;
                    System.out.println("✓ 已删除: " + name);
                } else {
                    System.out.println("✗ 删除失败: " + name);
                }
            }
            
            System.out.println("删除完成，共删除 " + deletedCount + " 个数据集");
            return deletedCount;
        } catch (Exception e) {
            System.err.println("删除所有数据集失败: " + e.getMessage());
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 检查RAGFlow服务状态
     */
    public void checkServiceStatus() {
        try {
            boolean available = ragflowService.isServiceAvailable();
            
            if (available) {
                System.out.println("✓ RAGFlow服务可用");
            } else {
                System.out.println("✗ RAGFlow服务不可用");
            }
        } catch (Exception e) {
            System.err.println("检查RAGFlow服务状态失败: " + e.getMessage());
        }
    }

    /**
     * 获取数据集中的文档列表
     * 
     * @param datasetName 数据集名称
     */
    public void listDatasetDocuments(String datasetName) {
        try {
            String datasetId = ragflowService.getDatasetIdByName(datasetName);
            
            if (datasetId == null || datasetId.isEmpty()) {
                System.out.println("未找到数据集: " + datasetName);
                return;
            }
            
            List<Map<String, Object>> documents = ragflowService.getDocuments(datasetId);
            
            if (documents == null || documents.isEmpty()) {
                System.out.println("数据集 " + datasetName + " 中没有文档");
                return;
            }
            
            System.out.println("数据集 " + datasetName + " 包含 " + documents.size() + " 个文档:");
            for (Map<String, Object> doc : documents) {
                System.out.println("  - " + doc.get("name") + " (ID: " + doc.get("id") + ")");
            }
            
        } catch (Exception e) {
            System.err.println("获取文档列表失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

